package com.linln.admin.miss.controller;

import com.linln.admin.miss.domain.Member;
import com.linln.admin.miss.service.MemberService;
import com.linln.admin.miss.validator.MemberValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小懒虫
 * @date 2021/10/18
 */
@Controller
@RequestMapping("/miss/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 登录
     * @param member
     * @return
     */
    @GetMapping("/login")
    @ResponseBody
    public ResultVo login(Member member){
       return memberService.login(member);
    }

    /**
     * 验证码登录
     * @param cellphone
     * @param code
     * @return
     */
    @GetMapping("/loginByCode")
    @ResponseBody
    public ResultVo loginByCode(String cellphone,String code){
        return memberService.loginByCode(cellphone,code);
    }

    /**
     * 发送验证码
     * @param cellphone
     * @return
     */
    @GetMapping("/sendCode")
    @ResponseBody
    public ResultVo sendCode(String cellphone){
        return memberService.sendCode(cellphone);
    }
    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("miss:member:index")
    public String index(Model model, Member member) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching();

        // 获取数据列表
        Example<Member> example = Example.of(member, matcher);
        Page<Member> list = memberService.getPageList(example);

        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/miss/member/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("miss:member:add")
    public String toAdd() {
        return "/miss/member/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("miss:member:edit")
    public String toEdit(@PathVariable("id") Member member, Model model) {
        model.addAttribute("member", member);
        return "/miss/member/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"miss:member:add", "miss:member:edit"})
    @ResponseBody
    public ResultVo save(@Validated MemberValid valid, Member member) {
        // 复制保留无需修改的数据
        if (member.getId() != null) {
            Member beMember = memberService.getById(member.getId());
            EntityBeanUtil.copyProperties(beMember, member);
        }

        // 保存数据
        memberService.save(member);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("miss:member:detail")
    public String toDetail(@PathVariable("id") Member member, Model model) {
        model.addAttribute("member",member);
        return "/miss/member/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("miss:member:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (memberService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}